package com.box.ai.common.tools;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.box.ai.mapper.CourseMapper;
import com.box.ai.mapper.CourseReservationMapper;
import com.box.ai.mapper.SchoolMapper;
import com.box.ai.pojo.entity.Course;
import com.box.ai.pojo.entity.CourseReservation;
import com.box.ai.pojo.entity.School;
import com.box.ai.pojo.query.CourseQuery;
import com.box.ai.service.CourseReservationService;
import com.box.ai.service.CourseService;
import com.box.ai.service.SchoolService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

@RequiredArgsConstructor
@Component
public class CourseTools {

    private final CourseMapper courseMapper;
    private final SchoolMapper schoolMapper;
    private final CourseReservationMapper courseReservationMapper;

    @Tool(description = "根据条件查询课程")
    public List<Course> queryCourse(@ToolParam(required = false, description = "课程查询条件") CourseQuery query) {
        if (query == null){
            return Collections.emptyList();
        }
        List<CourseQuery.Sort> sorts = query.getSorts();

        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq(query.getType() != null, "type", query.getType())
                .le(query.getEdu() != null, "edu", query.getEdu());
        if (!CollectionUtils.isEmpty(sorts)) {
            for (CourseQuery.Sort sort : sorts) {
                queryWrapper.orderBy(sort.getField()!=null, sort.getAsc(), sort.getField());
            }
        }
        return courseMapper.selectList(queryWrapper);
    }

    @Tool(description = "查询所有校区")
    public List<School> queryAllSchools() {
        return schoolMapper.selectAll();
    }

    @Tool(description = "生成课程预约单,并返回生成的预约单号")
    public String generateCourseReservation(
            @ToolParam(required = false, description = "预约课程名称")String courseName,
            @ToolParam(required = false, description = "学生姓名")String studentName,
            @ToolParam(required = false, description = "学生联系方式")String contactInfo,
            @ToolParam(required = false, description = "预约校区")String school,
            @ToolParam(required = false, description = "备注")String remark) {
        CourseReservation courseReservation = new CourseReservation();
        courseReservation.setCourse(courseName);
        courseReservation.setStudentName(studentName);
        courseReservation.setContactInfo(contactInfo);
        courseReservation.setSchool(school);
        courseReservation.setRemark(remark);
        courseReservationMapper.insert(courseReservation);
        return String.valueOf(courseReservation.getId());
    }
}